package com.bestcem.xm.ticket.dao.converter;

import com.bestcem.xm.ticket.entity.mongo.NotifySetting;
import com.bestcem.xm.ticket.entity.mongo.TicketNotifyConfig;
import com.bestcem.xm.ticket.entity.mongo.TicketRecord;
import com.bestcem.xm.ticket.entity.mysql.TicketNotifyConfigDO;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author zhangfeng <feng.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2021/9/22 11:36
 */
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class TicketNotifyConfigConverter {

    @Nullable
    public static TicketNotifyConfig doToDto(TicketNotifyConfigDO ticketNotifyConfigDO) {
        if (Objects.isNull(ticketNotifyConfigDO)) {
            return null;
        }

        TicketNotifyConfig ticketNotifyConfig = new TicketNotifyConfig();
        ticketNotifyConfig.setId(ticketNotifyConfigDO.getTicketNotifyConfigId());
        ticketNotifyConfig.setOrgId(ticketNotifyConfigDO.getOrgId());
        ticketNotifyConfig.setTicketConfigId(ticketNotifyConfigDO.getTicketConfigId());
        ticketNotifyConfig.setInstant(ticketNotifyConfigDO.getInstant());
        ticketNotifyConfig.setStatus(ticketNotifyConfigDO.getStatus());
        ticketNotifyConfig.setMode(ticketNotifyConfigDO.getMode());
        // 由于数据库历史数据有 0  [] [1] 多种类型，使用try catch防止出问题
        try {
            ticketNotifyConfig.setPeriod(CommonConverter.stringToList(ticketNotifyConfigDO.getPeriod(), Integer.class));
        } catch (Exception e) {
            log.error("period:{} 转换出错，尝试转换为Integer", ticketNotifyConfigDO.getPeriod());
            Integer period = CommonConverter.stringToObject(ticketNotifyConfigDO.getPeriod(), Integer.class);
            List<Integer> periodList = new ArrayList<>();
            periodList.add(period);
            ticketNotifyConfig.setPeriod(periodList);
        }

        ticketNotifyConfig.setEvent(ticketNotifyConfigDO.getEvent());
        ticketNotifyConfig.setHoursLimit(ticketNotifyConfigDO.getHoursLimit());
        ticketNotifyConfig.setHasReceiver(ticketNotifyConfigDO.getHasReceiver());
        ticketNotifyConfig.setHasCcList(ticketNotifyConfigDO.getHasCcList());
        ticketNotifyConfig.setReceiverList(CommonConverter
                .stringToList(ticketNotifyConfigDO.getReceiver(), TicketNotifyConfig.ReceiveUser.class));
        ticketNotifyConfig.setCcList(CommonConverter
                .stringToList(ticketNotifyConfigDO.getCcList(), TicketNotifyConfig.RoleRegion.class));
        ticketNotifyConfig.setCustomAttr(CommonConverter
                .stringToObject(ticketNotifyConfigDO.getCustomAttr(), TicketRecord.CustomAttr.class));
        ticketNotifyConfig.setWays(CommonConverter
                .stringToObject(ticketNotifyConfigDO.getWays(), NotifySetting.Way.class));
        ticketNotifyConfig.setSmtpId(ticketNotifyConfigDO.getSmtpId());
        ticketNotifyConfig.setEmailSubject(ticketNotifyConfigDO.getEmailSubject());
        ticketNotifyConfig.setEmailContent(ticketNotifyConfigDO.getEmailContent());
        ticketNotifyConfig.setEmailTemplateId(ticketNotifyConfigDO.getEmailTemplateId());
        ticketNotifyConfig.setSmsSubject(ticketNotifyConfigDO.getSmsSubject());
        ticketNotifyConfig.setSmsContent(ticketNotifyConfigDO.getSmsContent());
        ticketNotifyConfig.setSmsTemplateId(ticketNotifyConfigDO.getSmsTemplateId());
        ticketNotifyConfig.setSample(ticketNotifyConfigDO.getSample());

        CommonConverter.setEntity(ticketNotifyConfig, ticketNotifyConfigDO);
        return ticketNotifyConfig;
    }

    @Nonnull
    public static List<TicketNotifyConfig> doListToDtoList(List<TicketNotifyConfigDO> ticketNotifyConfigDOList) {
        List<TicketNotifyConfig> list = new ArrayList<>();
        if (Objects.isNull(ticketNotifyConfigDOList)) {
            return list;
        }

        for (TicketNotifyConfigDO ticketNotifyConfigDO : ticketNotifyConfigDOList) {
            TicketNotifyConfig ticketNotifyConfig = doToDto(ticketNotifyConfigDO);
            list.add(ticketNotifyConfig);
        }
        return list;
    }

    @Nullable
    public static TicketNotifyConfigDO dtoToDo(TicketNotifyConfig ticketNotifyConfig) {
        if (Objects.isNull(ticketNotifyConfig)) {
            return null;
        }

        TicketNotifyConfigDO ticketNotifyConfigDO = new TicketNotifyConfigDO();
        ticketNotifyConfigDO.setTicketNotifyConfigId(ticketNotifyConfig.getId());
        ticketNotifyConfigDO.setOrgId(ticketNotifyConfig.getOrgId());
        ticketNotifyConfigDO.setTicketConfigId(ticketNotifyConfig.getTicketConfigId());
        ticketNotifyConfigDO.setInstant(ticketNotifyConfig.getInstant());
        ticketNotifyConfigDO.setStatus(ticketNotifyConfig.getStatus());
        ticketNotifyConfigDO.setMode(ticketNotifyConfig.getMode());
        ticketNotifyConfigDO.setPeriod(CommonConverter.listToString(ticketNotifyConfig.getPeriod()));
        ticketNotifyConfigDO.setEvent(ticketNotifyConfig.getEvent());
        ticketNotifyConfigDO.setHoursLimit(ticketNotifyConfig.getHoursLimit());
        ticketNotifyConfigDO.setHasReceiver(ticketNotifyConfig.getHasReceiver());
        ticketNotifyConfigDO.setHasCcList(ticketNotifyConfig.getHasCcList());
        ticketNotifyConfigDO.setReceiver(CommonConverter.listToString(ticketNotifyConfig.getReceiverList()));
        ticketNotifyConfigDO.setCcList(CommonConverter.listToString(ticketNotifyConfig.getCcList()));
        ticketNotifyConfigDO.setCustomAttr(CommonConverter.objectToString(ticketNotifyConfig.getCustomAttr()));
        ticketNotifyConfigDO.setWays(CommonConverter.objectToString(ticketNotifyConfig.getWays()));
        ticketNotifyConfigDO.setSmtpId(ticketNotifyConfig.getSmtpId());
        ticketNotifyConfigDO.setSample(ticketNotifyConfig.getSample());
        ticketNotifyConfigDO.setEmailSubject(ticketNotifyConfig.getEmailSubject());
        ticketNotifyConfigDO.setEmailContent(ticketNotifyConfig.getEmailContent());
        ticketNotifyConfigDO.setEmailTemplateId(ticketNotifyConfig.getEmailTemplateId());
        ticketNotifyConfigDO.setSmsSubject(ticketNotifyConfig.getSmsSubject());
        ticketNotifyConfigDO.setSmsContent(ticketNotifyConfig.getSmsContent());
        ticketNotifyConfigDO.setSmsTemplateId(ticketNotifyConfig.getSmsTemplateId());
        ticketNotifyConfigDO.setSample(ticketNotifyConfig.getSample());

        CommonConverter.setDo(ticketNotifyConfig, ticketNotifyConfigDO);
        return ticketNotifyConfigDO;
    }
}
